package com.bkgy.component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bkgy.controller.planking.dao.TPdi1870HomeDao;
import com.bkgy.controller.planking.entity.dto.TPdi1870Code;
import com.bkgy.controller.planking.entity.dto.TPdi1870Home;
import com.bkgy.controller.planking.service.ITPdiCodeService;
import com.bkgy.sys.common.constant.CommonConstant;
import com.bkgy.sys.common.dao.IConstMapper;
import com.bkgy.sys.common.entity.SysConst;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 系统初始化组件，加载系统常量
 */
@Component
@RequiredArgsConstructor
public class InitComponent {
    private final IConstMapper constMapper;
    private final ITPdiCodeService defectService;
    private final TPdi1870HomeDao pdiHome1870Mapper;

    @PostConstruct
    public void init() {
        //缺陷代码
        List<TPdi1870Code> codeList = defectService.list();
        for (TPdi1870Code code : codeList) {
            CommonConstant.defectCodeMap.put(code.getDefectCode(), code.getDefectName());
        }

        //归户代码
        QueryWrapper<TPdi1870Home> w1 = new QueryWrapper<>();
        List<TPdi1870Home> tPdi1870Homes = pdiHome1870Mapper.selectList(w1);
        for (TPdi1870Home tPdi1870Home : tPdi1870Homes) {
            CommonConstant.tPdi1870HomeMap.put(tPdi1870Home.getCode(), tPdi1870Home.getCodeName());
        }

        //系统常量
        QueryWrapper<SysConst> wrapper = new QueryWrapper<>();
        List<SysConst> sysConsts = constMapper.selectList(wrapper);
        for (SysConst sysConst : sysConsts) {
            CommonConstant.constMap.put(sysConst.getConstKey(), sysConst.getConstValue());
        }
    }
}
